import 'package:ati_package/core/utils/ati_enum.dart';
import 'package:ati_package/pages/common/ati_scaffold.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:flutter_pickers/pickers.dart';
import 'package:flutter_pickers/style/picker_style.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../../../../core/utils/ati_font.dart';
import '../../../common/widgets/ati_common_btn.dart';
import '../../../common/widgets/ati_text_input.dart';

class UserAddressAddView extends StatefulWidget {
  const UserAddressAddView({super.key});

  @override
  State<UserAddressAddView> createState() => _UserAddressAddViewState();
}

class _UserAddressAddViewState extends State<UserAddressAddView> {
  final _formKey = GlobalKey<FormBuilderState>();

  @override
  Widget build(BuildContext context) {
    return AtiScaffold(
      title: '新建地址',
      body: Column(children: [_buildTextInput()]),
    );
  }

  _buildTextInput() {
    return Container(
      padding: EdgeInsets.only(top: 12.5.r, left: 16.r, right: 16.r),
      child: FormBuilder(
        key: _formKey,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            AtiTextInput(
              name: 'oldPassword',
              hint: '请填写收件人姓名',
              showClearButton: true,
              showPrefixIcon: true,
              prefixLabel: '收货人',
              validator: (val) =>
                  val == null || val.isEmpty ? '请填写收件人姓名' : null,
            ),
            AtiTextInput(
              name: 'newPassword',
              hint: '请填写手机号',
              showClearButton: true,
              showPrefixIcon: true,
              prefixLabel: '手机号',
              validator: (val) => val == null || val.isEmpty ? '请填写手机号' : null,
            ),
            AtiTextInput(
              name: 'country',
              hint: '请选择省市区',
              prefixLabel: '省市区',
              showPrefixIcon: true,
              inputType: AtiInputType.select,
              onTap: () {
                // 弹出选择器
                Pickers.showAddressPicker(
                  context,
                  pickerStyle: PickerStyle(title: Text('请选择所在地区')),
                  initProvince: '北京市', // 默认省
                  initCity: '北京市', // 默认市
                  initTown: '东城区', // 默认区
                  onConfirm: (p, c, t) {
                    debugPrint('选择结果：$p $c $t');
                  },
                );
              },
            ),
            AtiTextInput(
              name: 'address',
              hint: '请填写详细地址',
              prefixLabel: '详细地址',
              showClearButton: true,
              showPrefixIcon: true,
              maxLines: 4,
              // minLines: 2,
              onChanged: (v) {},
            ),
            _buildAefaultBtn(AtiSetModel.clearCache, '默认地址'),

            AtiCommonBtn(
              onPressed: () {},
              titleStr: '提交',
              margin: EdgeInsets.symmetric(horizontal: 22.r, vertical: 32.r),
            ),
          ],
        ),
      ),
    );
  }

  _buildAefaultBtn(AtiSetModel setModel, String titleStr) {
    return Container(
      height: 46.r,
      margin: EdgeInsets.only(top: 10.r),
      padding: EdgeInsets.symmetric(horizontal: 10.r),

      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(8.r),
      ),
      child: Row(
        children: [
          Text(
            titleStr,
            style: TextStyle(
              fontSize: AtiFont.font14,
              fontWeight: FontWeight.w600,
            ),
          ),
          Spacer(),
          SizedBox(
            width: 62.5.r, // 指定宽度
            height: 37.5.r, // 指定高度
            child: FittedBox(
              fit: BoxFit.fill,
              child: CupertinoSwitch(value: false, onChanged: (bool value) {}),
            ),
          ),
        ],
      ),
    );
  }
}
